package com.hp.service.impl;

import com.hp.context.Config;
import com.hp.dao.MenusDao;
import com.hp.entity.Menus;
import com.hp.entity.MenusTreeVo;
import com.hp.entity.RoleMenusVo;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class MenusServiceImpl implements MenusService {
    @Autowired
    private MenusDao menusDao;

    /**
     * 通过roleid查询对应的菜单
     *
     * @param id
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public List<Menus> findAllByRoleId(int id) {
        return menusDao.findAllByRoleId(id);
    }

    @Transactional(readOnly = true)
    @Override
    public List<MenusTreeVo> findAllByParent(List<Menus> list) {
        Menus menus = menusDao.findAllByParent();
        MenusTreeVo menusTreeVo = new MenusTreeVo(menus, list);
        List menuslist = new ArrayList();
        menuslist.add(menusTreeVo);
        return menuslist;
    }

    @Override
    public SubmitResultInfo authmenusByRole(List<RoleMenusVo> roleMenusVos) {

        menusDao.deleteAllRoleMenus(roleMenusVos.get(0).getRid());//删除该角色所有的授权数据

        menusDao.addRoleMenus(roleMenusVos);//重写授权

        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE, 666, new Object[]{"角色菜单权限"}));

    }
}
